<template>
  <div class="user-search">
    <el-form inline ref="formRef" :model="searchForm">
      <el-row :gutter="20">
        <template v-for="item in searchConfig.formItem" :key="item.prop">
          <el-col :span="8">
            <el-form-item :label="item.label" :placeholder="item.placeholder">
              <template v-if="item.type === 'Input'">
                <el-input
                  v-model="searchForm[item.prop]"
                  :placeholder="item.placeholder"
                ></el-input>
              </template>
              <template v-if="item.type === 'date-pickder'">
                <el-date-picker
                  v-model="searchForm[item.prop]"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                />
              </template>
              <template v-if="item.type === 'select'">
                <el-select v-model="searchForm[item.prop]" style="width: 100%">
                  <template v-for="option in item.options" :key="option.value">
                    <el-option :label="option.label" :value="option.value"></el-option>
                  </template>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <!-- 搜索和重置按钮 -->
    <div class="btns">
      <el-button plain @click="handleResetClick" icon="Refresh">重置</el-button>
      <el-button plain type="primary" icon="Search" @click="handleQueryClick">查询</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { ElForm } from 'element-plus'
// 接受的属性
interface IProps {
  searchConfig: {
    formItem: any[]
  }
}
// 定义自定义事件

const emit = defineEmits(['queryClick', 'resetClick'])
const initialForm: any = {}

const props = defineProps<IProps>()
// 遍历config文件中传来的变量
// for (const item in props.searchConfig.formItem) {
//   initialForm[item.prop] = ''
// }
props.searchConfig.formItem.forEach((item: any) => {
  initialForm[item.prop] = ''
})
const searchForm = reactive(initialForm)
const formRef = ref<InstanceType<typeof ElForm>>()
// 重置
const handleResetClick = () => {
  formRef.value?.resetFields()
  emit('resetClick')
}
// 查询
const handleQueryClick = () => {
  emit('queryClick', searchForm)
}
</script>

<style lang="less" scoped>
.user-search {
  background-color: #fff;
  padding: 20px;
}
.btns {
  text-align: right;
  padding: 0px 50px 10px 0;
}
</style>
